package com.chainyoung.component_tmperson.ui.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chainyoung.common.appcommon.entity.HdWalletTransaction;
import com.chainyoung.common.appcommon.hdwallet.EtherscanService;
import com.chainyoung.common.appcommon.hdwallet.ExchangeCalculator;
import com.chainyoung.common.appcommon.hdwallet.WalletServerConstant;
import com.chainyoung.common.base.SimpleFragment;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.common.utils.TimeUtils;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.router.RouterConstants;
import com.chainyoung.router.RouterUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.chainyoung.common.constant.CommonStringConstants.URL;

public class HdTransactionListAdapter extends BaseQuickAdapter<HdWalletTransaction, BaseViewHolder> {

    private String myAddr;
    SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat sd1 = new SimpleDateFormat("HH:mm");
    SimpleDateFormat sd2 = new SimpleDateFormat("yyyy-MM-dd");

    public HdTransactionListAdapter(@Nullable List<HdWalletTransaction> data, Context context, String myAddr) {
        super(R.layout.layout_hdwallet_transaction_item, data);
        mContext = context;
        this.myAddr = myAddr;
    }

    @Override
    protected void convert(BaseViewHolder helper, final HdWalletTransaction item) {

        boolean isSend = item.getFrom().equals(myAddr);

        helper.setText(R.id.tv_type, isSend ? mContext.getString(R.string.tmperson_sent) : mContext.getString(R.string.tmperson_recieved));

        Date transactionTime = new Date(item.getTimeStamp() * 1000);
        int isYesterday = 1;
        boolean isSuccessed = item.getIsError().equals("0");
        try {
            isYesterday = TimeUtils.isYeaterday(transactionTime, new Date());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        helper.setText(R.id.tv_time, isSuccessed ? isYesterday == -1 ? sd1.format(transactionTime) : isYesterday == 0 ? "昨天" : sd2.format(transactionTime) : "失败");
        helper.setTextColor(R.id.tv_time, isSuccessed ? ContextCompat.getColor(mContext, R.color._7e8da6) : ContextCompat.getColor(mContext, R.color._F95A54));

        ((ImageView) helper.getView(R.id.iv_type)).setImageResource(isSend ? R.mipmap.ic_type_send : R.mipmap.ic_type_recieve);

        //helper.setText(R.id.tv_recharge_num, item.getValue().divide(ExchangeCalculator.ONE_ETHER, 8, RoundingMode.DOWN).toPlainString());
        String str = isSend ? "-" : "+";
        helper.setText(R.id.tv_real_num, str + StringUtil.subZeroAndDot(item.getValue().divide(ExchangeCalculator.ONE_ETHER, 8, RoundingMode.DOWN).toPlainString()));
        helper.setTextColor(R.id.tv_real_num, isSend ? ContextCompat.getColor(mContext, R.color._333333) : ContextCompat.getColor(mContext, R.color._4486f5));

        String toAddr= TextUtils.isEmpty(item.getTo())?item.getContractAddress():item.getTo();//有可能是给合约地址转账
        helper.setText(R.id.tv_addr, isSend ? "发到:" + toAddr.substring(0, 4) + "...." +toAddr.substring(toAddr.length() - 4)
                : "来自:" + item.getFrom().substring(0, 4) + "...." + item.getFrom().substring(item.getFrom().length() - 4));

        helper.getView(R.id.rl_transaction).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = String.format(WalletServerConstant.GET_TRANSACTION_BY_HASH_URL + item.getHash());
                RouterUtils.getInstance()
                        .build(RouterConstants.PATH_OTHERAPP)
                        .withString("title", url)
                        .withString(URL, url)
                        .navigation(mContext);
            }
        });

        //  helper.setText(R.id.tv_status, item.getIsError().equals("0") ? "已完成" : "已失败");
        //  helper.setTextColor(R.id.tv_status, item.getIsError().equals("0") ? ContextCompat.getColor(mContext, R.color._0AC073) : ContextCompat.getColor(mContext, R.color._F95A54));
    }
}
